﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Model.CSP;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Common.Factories;
using CSP.Dao;
using System.Collections.ObjectModel;

namespace CSP.Bll.ObjectBlls
{
    public class ClassTypeBll : CSPCommonBll, IClassTypeBll
    {
        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEYALL = "ClassTypeAll";

        private IClassTypeDao _classTypeDao;

        public ClassTypeBll()
        {
            _cacheProvider = CachedProviderFactory.GetLocalCachedProvider();
            _classTypeDao = CSPDaoFactory.GetInstance().GetClassTypeDao();
        }

        public ClassType GetDataById(int id)
        {
            ClassType classType = GetById(id);
            if (classType == null)
            {
                FindAll();
                classType = GetById(id);
            }

            return classType;
        }

        public ICollection<ClassType> GeDatatByIds(int[] Ids)
        {
            throw new NotImplementedException();
        }

        public void Add(ClassType obj)
        {
            if (obj == null)
            {
                throw new Exception("AgeGroup Can't be null");
            }
            _classTypeDao.Add(obj);

            //清除缓存
            this.RefreshCache();
        }

        public void Update(ClassType obj)
        {
            if (obj == null)
            {
                throw new Exception("ClassType Can't be null");
            }
            _classTypeDao.Update(obj);

            //清除缓存0
            this.RefreshCache();
        }

        public void DeleteById(int id)
        {
            _classTypeDao.DeleteById(id);

            //清除缓存0
            this.RefreshCache();
        }

        public void RefreshCache()
        {
            //删除缓存Key
            _cacheProvider.Remove(CACHEKEYALL);
        }

        public ICollection<ClassType> FindAll()
        {
            //检测缓存是否存在
            ICollection<ClassType> classTypes = _cacheProvider.GetData<ClassType[]>(CACHEKEYALL);
            if (classTypes == null)
            {
                //从数据库读取数据
                classTypes = _classTypeDao.FindAll();
                _cacheProvider.AddData(CACHEKEYALL, classTypes, DateTime.Now.AddHours(10));
            }
            return classTypes;
        }

        /// <summary>
        /// 通过Id 获取开课类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private ClassType GetById(int id)
        {
            ClassType classType = null;

            //检测缓存是否存在
            ICollection<ClassType> classTypes = GetByIds(new int[] { id });
            if (classTypes != null && classTypes.Count != 0)
            {
                classType = classTypes.FirstOrDefault();
            }
            return classType;
        }

        private ICollection<ClassType> GetByIds(int[] ids)
        {
            ICollection<ClassType> result = new Collection<ClassType>();

            //检测缓存是否存在
            ICollection<ClassType> classTypes = _cacheProvider.GetData<ClassType[]>(CACHEKEYALL);
            if (classTypes == null)
            {
                return null;
            }
            foreach (var item in classTypes)
            {
                if (ids.Contains(item.Id))
                {
                    result.Add(item);
                }
            }
            return result;
        }
    }
}
